<?php
require_once '../app.render.php';
$smarty->compile_dir = '../templates_c'; 



if($_GET['install'] == 1){
	//ALTER TABLE `order_sum` ADD `pos` INT( 11 ) NOT NULL DEFAULT '0'
	$database->query("
					ALTER TABLE `locations` ADD `password` VARCHAR( 255 ) NOT NULL ");
	echo "Database updated";exit;
}
if($_GET['logout'] == 1){
	
	unset($_SESSION['pos']);
	header("Location: index.php");
}


if($_SESSION['pos'] == ""){
	
	
	
	$user = 0;
	
	
	
	if($_POST['login'] != ""){
		
	$find_user = $database->select("SELECT * FROM locations where email  = '".$_POST['username']."' AND password = '".md5($_POST['password'])."'");
	
		if($find_user != FALSE){
		
		
			$_SESSION['pos'] = $find_user[0]['rid'];
			$_SESSION['pos_location'] = $find_user[0]['id'];
			header("Location: index.php");
			
		}else{
			
			$error = 'Please check your login and try again';
			
		}
		
	}
	
	
}else{
	
	
	$user = $database->select("SELECT * FROM config where id = ".$_SESSION['pos']." ");

	$orders = $database->select("SELECT * FROM order_sum WHERE rid = ".$_SESSION['pos']." and lid = ".$_SESSION['pos_location']." AND  DATE(date) = DATE(NOW()) AND (pos = 1 or pos = 0) AND status = 1 ORDER by date asc");
							 
						
						if($orders != FALSE){
						for($i=0; $i<count($orders); $i++){
				
							$orders[$i]['user'] = $database->select("SELECT * FROM users where id = ".$orders[$i]['uid']."");
							
						}
						}
							 
							 
							 
							 
							 
							 
							 
	$orders_done = $database->select("SELECT * FROM order_sum WHERE rid = ".$_SESSION['pos']." and lid = ".$_SESSION['pos_location']." AND  DATE(date) = DATE(NOW())  AND pos = 1  AND status = 1 ORDER by date desc ");							 
							if($orders_done != FALSE){
						for($j=0; $j<count($orders); $j++){
				
							$orders_done[$j]['user'] = $database->select("SELECT * FROM users where id = ".$orders[$j]['uid']."");
							
						}
							}



$smarty->assign("orders",$orders);
$smarty->assign("orders_done",$orders_done);


}

$smarty->assign("locations",$locations);
$smarty->assign("error",$error);
$smarty->assign("user",$user);
$smarty->display(''.$root.'pos/template/index.tpl');
?>
